package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panshi.modules.domain.*;
import com.panshi.modules.domain.UserMember;
import com.panshi.modules.mapper.UserDynamicMapper;
import com.panshi.modules.service.UserDynamicService;
import com.panshi.modules.service.UserService;
import com.panshi.vo.ItemQueryUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDynamicServiceImpl extends ServiceImpl<UserDynamicMapper, UserDynamic> implements UserDynamicService {
    @Autowired
    private UserService userService;

    //查询个人动态所有信息
    @Override
    public List<UserDynamicRelevance> findUserDynamicList(ItemQueryUserVo itemQueryVo) {
        return this.baseMapper.findUserDynamicList(itemQueryVo);
    }

    //查询个人动态里问题信息
    @Override
    public List<UserDynamicQuestion> findQaQuestionDynamicList(ItemQueryUserVo itemQueryVo) {
        return  this.baseMapper.findQaQuestionDynamicList(itemQueryVo);
    }

    //查询个人动态里回答信息
    @Override
    public List<UserDynamicAnswer> findQaAnswerDynamicList(ItemQueryUserVo itemQueryVo) {
        return this.baseMapper.findQaAnswerDynamicList(itemQueryVo);
    }

    //查询个人动态里资讯信息
    @Override
    public List<UserDynamicZx> findZxDynamicList(ItemQueryUserVo itemQueryVo) {
        return this.baseMapper.findZxDynamicList(itemQueryVo);
    }
    //删除个人动态指定的未审核信息
    @Override
    public int deleteUserDynamic(int id) {
        //将其修改为逻辑删除
        UserDynamic userDynamicUpdate = new UserDynamic();
        userDynamicUpdate.setId(id);
        userDynamicUpdate.setDeleted(0);
        UserDynamicRelevance userDynamicRelevance= this.baseMapper.selectUserDynamicId(id);
        if(userDynamicRelevance.getUncheckType()==1){
            //new   ItemQueryUserVo类进行修改用户数量
           ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
            itemQueryUserVo.setId((long) userDynamicRelevance.getUserId());
            //查询该动态ID下的用户信息
            UserMember userMemberQuery = userService.selectByUserInfo(userDynamicRelevance.getUserId());
            if(10==userDynamicRelevance.getType()){//修改文章数量
                itemQueryUserVo.setArticleNum(userMemberQuery.getArticleNum()-1);
            }else if(20==userDynamicRelevance.getType()){//修改问题数量
                itemQueryUserVo.setQuestionNum(userMemberQuery.getQuestionNum()-1);
            }else if(30==userDynamicRelevance.getType()){//修改回答数量
                itemQueryUserVo.setAnswerNum(userMemberQuery.getAnswerNum()-1);
            }
            //修改信息
            userService.updateUserInfo(itemQueryUserVo);
        }


        return this.baseMapper.updateUserDynamicById(userDynamicUpdate);
    }

    @Override
    public List<UserDynamicCount> selectResultMapCount(ItemQueryUserVo itemQueryVo) {
        List<UserDynamicCount> mapList = this.baseMapper.selectResultMapCount(itemQueryVo);
        int a = 0;
        int b = 0;
        int c = 0;
        for (UserDynamicCount userDynamicCount:mapList){
            if(10 == userDynamicCount.getType()){
                a++;
            }else if(20 == userDynamicCount.getType()){
                b++;
            }else if(30 == userDynamicCount.getType()){
                c++;
            }
        }

        if(a==0){
            UserDynamicCount userDynamicCount = new UserDynamicCount();
            userDynamicCount.setType(10);
            userDynamicCount.setCount(0);
            mapList.add(userDynamicCount);
        }
        if(b==0){
            UserDynamicCount userDynamicCount = new UserDynamicCount();
            userDynamicCount.setType(20);
            userDynamicCount.setCount(0);
            mapList.add(userDynamicCount);
        }
        if(c==0){
            UserDynamicCount userDynamicCount = new UserDynamicCount();
            userDynamicCount.setType(30);
            userDynamicCount.setCount(0);
            mapList.add(userDynamicCount);
        }
        return mapList;
    }
}
